package com.example.demo.RabbitMq;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    // 交换机名称
    public static final String REGISTER_EXCHANGE = "user.register.exchange";
    
    // 队列名称
    public static final String EMAIL_QUEUE = "user.email.send.queue";

    // 配置JSON消息转换器
    @Bean
    public Jackson2JsonMessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    // 声明Fanout交换机
    @Bean
    public FanoutExchange registerExchange() {
        // 持久化交换机
        return new FanoutExchange(REGISTER_EXCHANGE);
    }



    // 声明邮件发送队列
    @Bean
    public Queue emailQueue() {
        return new Queue(EMAIL_QUEUE, false);
    }


    // 绑定邮件队列到交换机
    @Bean
    public Binding emailBinding(FanoutExchange registerExchange, Queue emailQueue) {
        return BindingBuilder.bind(emailQueue).to(registerExchange);
    }



}
    